package com.org.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.org.model.Conferenceroom;
import com.org.model.User;
import com.org.service.impl.ConferenceroomServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author test
 * @since 2021-06-05
 */
@CrossOrigin
@RestController
@RequestMapping("/conferenceroom")
public class ConferenceroomController {
    @Autowired
    private ConferenceroomServiceImpl service;

    @PostMapping("/selectAllRoom")
    public Map<String,Object> selectAllRoom(){
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","success");
        map.put("data",service.selectAllRoom());
        return map;
    }

    @PostMapping("/selectAllRoomPage")
    public Map<String,Object> selectAllRoomPage(
            @RequestParam(name = "current") Integer current,
            @RequestParam(name = "size") Integer size
    ){
        Map<String,Object> map = new HashMap<>();
        Page<Conferenceroom> roomPage = new Page<>(current,size);
        QueryWrapper<Conferenceroom> queryWrapper = new QueryWrapper<>();
        IPage<Conferenceroom> data = service.page(roomPage,queryWrapper);
        map.put("code",200);
        map.put("msg","success");
        map.put("data",data);
        return map;
    }

    @PostMapping("/selectIsUsePage")
    public Map<String,Object> selectIsUsePage(
            @RequestParam(name = "current") Integer current,
            @RequestParam(name = "size") Integer size
    ){
        Map<String,Object> map = new HashMap<>();
        Page<Conferenceroom> roomPage = new Page<>(current,size);
        IPage<Conferenceroom> data = service.selectIsUsePage(roomPage);
        map.put("code",200);
        map.put("msg","success");
        map.put("data",data);
        return map;
    }

    @PostMapping("/getRoomById")
    public Map<String,Object> getRoomById(@RequestParam Integer id){
        Conferenceroom room = service.getByCid(id);
        Map<String,Object> data = new HashMap<>();
        if (room != null){
            data.put("code",200);
            data.put("msg","查询成功");
            data.put("data",room);
            return data;
        }
        data.put("code",400);
        data.put("msg","未查询到数据");
        return data;
    }

    @DeleteMapping("/deleteById")
    public Map<String,Object> deleteById(@RequestParam Integer id){
        Map<String,Object> data = new HashMap<>();
        boolean check = service.removeById(id);
        // boolean check = serviceImp.deleteById(id);
        if (check){
            data.put("code",200);
            data.put("msg","删除成功");
            return data;
        }
        data.put("code",400);
        data.put("msg","删除失败");
        return data;
    }

    @PostMapping("/insert")
    public Map<String,Object> insertUserData(Conferenceroom room){
        boolean check = false;
        Map<String,Object> data = new HashMap<>();
        data.put("code",200);
        String name = room.getName();
        Conferenceroom conferenceroom = service.getIdByName(name);
        if (conferenceroom==null){
            check = service.save(room);
            if (check){
                data.put("msg","新增成功");
            }else {
                data.put("msg","操作失败");
            }
        } else {
            data.put("msg","操作失败,会议室已存在");
        }
        return data;

    }

    @PostMapping("/update")
    public Map<String,Object> updateById(Conferenceroom room){
        Map<String,Object> data = new HashMap<>();
        boolean check =  service.updateById(room);
        data.put("code",200);
        if (check ){
            data.put("msg","更新成功");
        }else {
            data.put("msg","更新失败");
        }
        return data;
    }
}
